export class TreeUtil {
    static changeTreePositionData = (tree, key, node) => {
        for (let index = 0; index < tree.length; index++) {
            if (tree[index].key === key) {// 根节点判定
                tree[index] = node
                return tree
            }
            const searchId = (source, key,node) => {// 开启递归
                if (source == null)
                for (let i = 0; i < source.length; i++) {
                    let item = source[i]
                    if (item.key === key) {
                        item = node
                        return
                    }
                    if (item.children) { // 只对非末端节点进行递归
                        searchId(item.children, key,node)
                    }
                }
            }
            searchId(tree[index].children, key,node)
        }
        return tree
    }

    static changeTreePositionSelected = (tree, key) => {
        //全部关闭选择
        for (let index = 0; index < tree.length; index++) {
            tree[index].selected = false;
            const searchId = (parent, key) => {// 开启递归
                if (parent != null)
                    for (let i = 0; i < parent.children.length; i++) {
                        let item = parent.children[i]
                        item.selected = false
                        if (item.children) { // 只对非末端节点进行递归
                            searchId(item, key)
                        }
                    }
            }
            searchId(tree[index], key)
        }
        //选中节点且展开
        for (let index = 0; index < tree.length; index++) {
            if (tree[index].key === key) {// 根节点判定
                tree[index].selected = true;
                return true;
            }
            const searchId = (source, key):boolean => {// 开启递归
                if (source != null)
                    for (let i = 0; i < source.length; i++) {
                        let item = source[i]
                        if (item.key === key) {
                            item.selected = true
                            return true;
                        }
                        if (item.children) { // 只对非末端节点进行递归
                            var b1 = searchId(item.children, key);
                            if (b1){
                                item.expanded = true;
                                return true;
                            }
                        }else {
                            return false;
                        }
                    }
            }
            var b = searchId(tree[index].children, key);
            if (b){
                tree[index].expanded = true;
            }
        }
        return tree
    }

    static deleteTreePositionData = (tree, key) => {
        for (let index = 0; index < tree.length; index++) {
            if (tree[index].key === key) {// 根节点判定
                tree.splice(index, 1)
                return tree
            }
            const searchId = (source, key) => {// 开启递归
                for (let i = 0; i < source.length; i++) {
                    let item = source[i]
                    if (item.key === key) {
                        source.splice(i, 1)
                        return
                    }
                    if (item.children) { // 只对非末端节点进行递归
                        searchId(item.children, key)
                    }
                }
            }
            searchId(tree[index].children, key)
        }
        return tree
    }

    static initExpandMap = (tree, expandMap) => {
        for (let index = 0; index < tree.length; index++) {
            expandMap[tree[index].key] = tree[index].expanded
            const searchId = (source, map) => {// 开启递归
                for (let i = 0; i < source.length; i++) {
                    let item = source[i]
                    map[item.key] = item.expanded
                    if (item.children) { // 只对非末端节点进行递归
                        searchId(item.children, expandMap)
                    }
                }
            }
            searchId(tree[index].children, expandMap)
        }
        return expandMap
    }

    static revertExpandMap = (tree, expandMap) => {
        for (let index = 0; index < tree.length; index++) {
            tree[index].expanded = expandMap[tree[index].key];
            const searchId = (source, map) => {// 开启递归
                for (let i = 0; i < source.length; i++) {
                    let item = source[i]
                    item.expanded = map[item.key]
                    if (item.children) { // 只对非末端节点进行递归
                        searchId(item.children, map)
                    }
                }
            }
            searchId(tree[index].children, expandMap)
        }
        return tree
    }
}
